package com.naicheng.ivote.adapter;

import java.util.ArrayList;
import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.doman.HotMainlistItemDetailBean;
import com.naicheng.ivote.uiself.ToastUtils;
import com.naicheng.ivote.views.HotDetailMainActivity;
import com.naicheng.ivote.views.HotDetailVoteMainActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HotMainListAdapter extends BaseAdapter{

	
	ViewHolder holder = null;
	private ArrayList<HotMainlistItemDetailBean> list;
	Activity context;
	Handler handler;
	private String flag;

	public HotMainListAdapter() {
		super();
		// TODO Auto-generated constructor stub
	}
	public HotMainListAdapter(ArrayList<HotMainlistItemDetailBean> list,Activity context,Handler handler,String flag) {
		this.list=list;
		this.context=context;
		this.handler=handler;
		this.flag=flag;
		
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.hotmain_listitem, null);
			holder.main_hottype_item_title_tv = (TextView) convertView.findViewById(R.id.main_hottype_item_title_tv);
			holder.main_hottype_item_content_tv = (TextView) convertView.findViewById(R.id.main_hottype_item_content_tv);
			holder.main_hottype_item_option_tv = (TextView) convertView.findViewById(R.id.main_hottype_item_option_tv);
			holder.main_hottype_item_sex1_tv = (TextView) convertView.findViewById(R.id.main_hottype_item_sex1_tv);
			holder.main_hottype_item_usernames_tv = (TextView) convertView.findViewById(R.id.main_hottype_item_usernames_tv);
			holder.main_hot_praise_but = (Button) convertView.findViewById(R.id.main_hot_praise_but);
			holder.main_hot_comment_but = (Button) convertView.findViewById(R.id.main_hot_comment_but);
			holder.main_hot_report_but = (Button) convertView.findViewById(R.id.main_hot_report_but);
			holder.main_hot_share_but = (Button) convertView.findViewById(R.id.main_hot_share_but);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(list.get(position).isAnswered()){
			holder.main_hottype_item_title_tv.setTextColor(context.getResources().getColor(R.color.hui1));
		}else{
			holder.main_hottype_item_title_tv.setTextColor(context.getResources().getColor(R.color.bai1));
		}
		
		holder.main_hottype_item_title_tv.setText(list.get(position).getTitle());
		holder.main_hottype_item_content_tv.setText(list.get(position).getContent());
		holder.main_hottype_item_option_tv.setText(list.get(position).getOptionnumber()+"选"+list.get(position).getAnswernumber());
		if("f".equals(list.get(position).getSex())){
			holder.main_hottype_item_sex1_tv.setText(R.string.list_item_sex2);
			holder.main_hottype_item_sex1_tv.setTextColor(context.getResources().getColor(R.color.sex_zi));
		}else{
			holder.main_hottype_item_sex1_tv.setText(R.string.list_item_sex1);
			holder.main_hottype_item_sex1_tv.setTextColor(context.getResources().getColor(R.color.sex_lan));
		}
		holder.main_hottype_item_usernames_tv.setText(list.get(position).getAccount());
		
		holder.main_hot_praise_but.setText("赞 "+list.get(position).getPraise());
		System.out.println(list.get(position).isPraised());
		if(list.get(position).isPraised()){
			holder.main_hot_praise_but.setTextColor(context.getResources().getColor(R.color.hotdetail_but_lv));
		}else{
			holder.main_hot_praise_but.setTextColor(context.getResources().getColor(R.color.bai1));
		}
		
		holder.main_hot_comment_but.setText("评论 "+list.get(position).getComment());
		if(list.get(position).isCommented()){
			holder.main_hot_comment_but.setTextColor(context.getResources().getColor(R.color.hotdetail_but_lv));
		}else{
			holder.main_hot_comment_but.setTextColor(context.getResources().getColor(R.color.bai1));
		}
		
		if(list.get(position).isReported()){
			holder.main_hot_report_but.setTextColor(context.getResources().getColor(R.color.hotdetail_but_lv));
		}else{
			holder.main_hot_report_but.setTextColor(context.getResources().getColor(R.color.bai1));
		}
		
		holder.main_hottype_item_title_tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ConstantMine.hotItemId=list.get(position).getId();
				if(list.get(position).isAnswered()){
					context.startActivity(new Intent(context,HotDetailVoteMainActivity.class));
				}else{
					context.startActivity(new Intent(context,HotDetailMainActivity.class));
				}
				
			}
		});
		holder.main_hottype_item_content_tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ConstantMine.hotItemId=list.get(position).getId();
				if(list.get(position).isAnswered()){
					context.startActivity(new Intent(context,HotDetailVoteMainActivity.class));
				}else{
					context.startActivity(new Intent(context,HotDetailMainActivity.class));
				}
				
			}
		});
		
		
		holder.main_hot_praise_but.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LinearLayout Parentll=(LinearLayout) v.getParent();
				Button but =(Button) Parentll.getChildAt(0);
				
				Message mes=new Message();
				Bundle data=new Bundle();
				data.putInt("id", list.get(position).getId());
				mes.setData(data);
				
				if(list.get(position).isPraised()){
					mes.what=1;
					handler.sendMessage(mes);
					list.get(position).setPraised(false);
					ToastUtils.getDiyToast(context, -50,"  赞 -1  ");
					list.get(position).setPraise(list.get(position).getPraise()-1);
					but.setText("赞 "+list.get(position).getPraise());
					but.setTextColor(context.getResources().getColor(R.color.bai1));
				}else{
					mes.what=2;
					handler.sendMessage(mes);
					list.get(position).setPraised(true);
					ToastUtils.getDiyToast(context, -50,"  赞+1  ");
					list.get(position).setPraise(list.get(position).getPraise()+1);
					but.setText("赞 "+list.get(position).getPraise());
					but.setTextColor(context.getResources().getColor(R.color.hotdetail_but_lv));
				}
				
			}
		});
		
		holder.main_hot_comment_but.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if(list.get(position).isAnswered()){
					LinearLayout Parentll=(LinearLayout) v.getParent();
					Button but =(Button) Parentll.getChildAt(1);
					
					Message mes=new Message();
					Bundle data=new Bundle();
					data.putInt("id", list.get(position).getId());
					mes.setData(data);
					mes.what=3;
					handler.sendMessage(mes);
					
					if(!list.get(position).isCommented()){
						list.get(position).setCommented(true);
						list.get(position).setComment(list.get(position).getComment()+1);
//						but.setText("评论  "+list.get(position).getComment());
						but.setTextColor(context.getResources().getColor(R.color.hotdetail_but_lv));
					}
				}else{
					ToastUtils.getDiyToast(context, -50, "亲，先投票，后评论");
				}
				
				
				
			}
		});
		holder.main_hot_report_but.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LinearLayout Parentll=(LinearLayout) v.getParent();
				Button but =(Button) Parentll.getChildAt(2);
				
				Message mes=new Message();
				Bundle data=new Bundle();
				data.putInt("id", list.get(position).getId());
				mes.setData(data);
				
				
				if(list.get(position).isReported()){
					mes.what=4;
					handler.sendMessage(mes);
					list.get(position).setReported(false);
					ToastUtils.getDiyToast(context, -50,"  取消举报成功！  ");
					but.setTextColor(context.getResources().getColor(R.color.bai1));
				}else{
					mes.what=5;
					handler.sendMessage(mes);
					list.get(position).setReported(true);
					ToastUtils.getDiyToast(context, -50,"  举报成功！  ");
					but.setTextColor(context.getResources().getColor(R.color.hotdetail_but_lv));
				}
				
			}
		});
		
		holder.main_hot_share_but.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LinearLayout Parentll=(LinearLayout) v.getParent();
				Button but =(Button) Parentll.getChildAt(3);
				
				Message mes=new Message();
				Bundle data=new Bundle();
				data.putInt("id", list.get(position).getId());
				mes.setData(data);
				mes.what=6;
				handler.sendMessage(mes);
				
				
			}
		});
		
		
		return convertView;
	}
	
	static class ViewHolder {
		TextView main_hottype_item_title_tv;
		TextView main_hottype_item_content_tv;
		TextView main_hottype_item_option_tv;
		TextView main_hottype_item_sex1_tv;
		TextView main_hottype_item_usernames_tv;
		Button main_hot_praise_but;
		Button main_hot_comment_but;
		Button main_hot_report_but;
		Button main_hot_share_but;
	}

	public void net(){
		
	}
	
}
